package my.jwds.p191;

public class HammingWeight {

    public static void main(String[] args) {
        HammingWeight o = new HammingWeight();
        System.out.println(o.hammingWeight(-1));
    }


    public int hammingWeight(int n) {
        if (n == Integer.MIN_VALUE)return 1;
        if (n == Integer.MIN_VALUE+1)return 2;
        boolean flag = n < 0;
        if (flag){
            n *= -1;
            n--;
        }
        int oneNum = 0;
        while(n != 0){
            if (n % 2 == 1){
                oneNum++;
            }
            n /= 2;
        }
        if (flag){
            oneNum = 31-oneNum;
            oneNum++;
        }
        return oneNum;
    }

}
